package Hxw3DEngine.core.Resource.texture
{
	import Hxw3DEngine.asset.parsers.texture.CubeBitmapVO;
	import Hxw3DEngine.utils.Stage3dUtils;
	import Hxw3DEngine.core.Resource.texture.ResourceTexture;
	import flash.display.BitmapData;
	import flash.display3D.Context3D;
	import flash.display3D.textures.CubeTexture;
	import flash.display3D.textures.TextureBase;
	import flash.display3D.Context3DTextureFormat;
	import Hxw3DEngine.proxy.Stage3DProxy;
	
	public class CubeBitmapTexture extends ResourceTexture
	{
		private var bitmapDatas:Vector.<BitmapData>;
		
		// 兼容以前接口
		static public function create(_posX:BitmapData, _negX:BitmapData, _posY:BitmapData,
			_negY:BitmapData,_posZ:BitmapData, _negZ:BitmapData,_mipmap:Boolean = true):CubeBitmapTexture
		{
			var bt:CubeBitmapTexture = new CubeBitmapTexture();
			bt.createFromVO(new CubeBitmapVO(_posX, _negX, _posY, _negY, _posZ, _negZ, true));
			return bt;
		}	
		
		public function CubeBitmapTexture()
		{
			typeMode = TYPE_CUBE;
			wrapMode = WRAP_CLAMP;
		}
		
		override public function createFromVO(vo:Object):Boolean
		{
			var voCbt:CubeBitmapVO = vo as CubeBitmapVO;
			bitmapDatas = voCbt.bitmapDatas;
			_width = bitmapDatas[0].width;
			_height = bitmapDatas[0].height;
			mipmap = voCbt.mipmap;
			format = Context3DTextureFormat.BGRA;
			
			texture = Stage3DProxy.context3d.createCubeTexture(width, format, false);
			if (texture == null)
			{
				return false;
			}
			var i:int = 0;
			if (mipmap)
			{
				for (i = 0; i < 6; i++)
				{
					Stage3dUtils.uploadCubeMipMap(texture as CubeTexture, bitmapDatas[i], i);
				}
			}
			else
			{
				for (i = 0; i < 6; i++)
				{
					CubeTexture(texture).uploadFromBitmapData(bitmapDatas[i], i);
				}
			}
			return true;
		}
		
		override public function dispose():void
		{
			super.dispose();
			for (var i:int = 0; i < 6; i++)
			{
				bitmapDatas[i].dispose();
			}
			bitmapDatas.length = 0;
			bitmapDatas = null;
		}
		
	}
}